package com.hamizhong.iflowers.web.action.admin.prodgoods;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import com.hamizhong.iflowers.service.IFestivalService;
import com.hamizhong.iflowers.service.IFlowerCountService;
import com.hamizhong.iflowers.service.IFlowerFloridService;
import com.hamizhong.iflowers.service.IGoodsFestivalMapService;
import com.hamizhong.iflowers.service.IGoodsProductMapService;
import com.hamizhong.iflowers.service.IGoodsPurposeMapService;
import com.hamizhong.iflowers.service.IGoodsService;
import com.hamizhong.iflowers.service.IGoodsTargetMapService;
import com.hamizhong.iflowers.service.IProductService;
import com.hamizhong.iflowers.service.IPurposeService;
import com.hamizhong.iflowers.service.ITargetService;
import com.hamizhong.iflowers.utils.MyTools;
import com.hamizhong.iflowers.web.base.MyBaseAction;
import com.hamizhong.iflowers.web.dto.IOPResultDto;
import com.hamizhong.iflowers.web.vo.FestivalVO;
import com.hamizhong.iflowers.web.vo.FlowerCountVO;
import com.hamizhong.iflowers.web.vo.FlowerFloridVO;
import com.hamizhong.iflowers.web.vo.GoodsFestivalMapVO;
import com.hamizhong.iflowers.web.vo.GoodsProductMapVO;
import com.hamizhong.iflowers.web.vo.GoodsPurposeMapVO;
import com.hamizhong.iflowers.web.vo.GoodsTargetMapVO;
import com.hamizhong.iflowers.web.vo.GoodsVO;
import com.hamizhong.iflowers.web.vo.IObjectVO;
import com.hamizhong.iflowers.web.vo.ProductVO;
import com.hamizhong.iflowers.web.vo.PurposeVO;
import com.hamizhong.iflowers.web.vo.TargetVO;

public class DoGoodsUpdateAction extends MyBaseAction {
	private IGoodsService iGoodsService = null;
	private IProductService iProductService = null;
	private IPurposeService iPurposeService = null;
	private IFestivalService iFestivalService = null;
	private ITargetService iTargetService = null;
	private IFlowerFloridService iFlowerFloridService = null;
	private IFlowerCountService iFlowerCountService = null;
	private IGoodsProductMapService iGoodsProductMapService = null;
	private IGoodsPurposeMapService iGoodsPurposeMapService = null;
	private IGoodsFestivalMapService iGoodsFestivalMapService = null;
	private IGoodsTargetMapService iGoodsTargetMapService = null;
	
	public IGoodsService getiGoodsService() {
		return iGoodsService;
	}
	public void setiGoodsService(IGoodsService iGoodsService) {
		this.iGoodsService = iGoodsService;
	}
	public IProductService getiProductService() {
		return iProductService;
	}
	public void setiProductService(IProductService iProductService) {
		this.iProductService = iProductService;
	}
	public IPurposeService getiPurposeService() {
		return iPurposeService;
	}
	public void setiPurposeService(IPurposeService iPurposeService) {
		this.iPurposeService = iPurposeService;
	}
	public IFestivalService getiFestivalService() {
		return iFestivalService;
	}
	public void setiFestivalService(IFestivalService iFestivalService) {
		this.iFestivalService = iFestivalService;
	}
	public ITargetService getiTargetService() {
		return iTargetService;
	}
	public void setiTargetService(ITargetService iTargetService) {
		this.iTargetService = iTargetService;
	}
	public IFlowerFloridService getiFlowerFloridService() {
		return iFlowerFloridService;
	}
	public void setiFlowerFloridService(IFlowerFloridService iFlowerFloridService) {
		this.iFlowerFloridService = iFlowerFloridService;
	}
	public IFlowerCountService getiFlowerCountService() {
		return iFlowerCountService;
	}
	public void setiFlowerCountService(IFlowerCountService iFlowerCountService) {
		this.iFlowerCountService = iFlowerCountService;
	}
	public IGoodsProductMapService getiGoodsProductMapService() {
		return iGoodsProductMapService;
	}
	public void setiGoodsProductMapService(
			IGoodsProductMapService iGoodsProductMapService) {
		this.iGoodsProductMapService = iGoodsProductMapService;
	}
	public IGoodsPurposeMapService getiGoodsPurposeMapService() {
		return iGoodsPurposeMapService;
	}
	public void setiGoodsPurposeMapService(
			IGoodsPurposeMapService iGoodsPurposeMapService) {
		this.iGoodsPurposeMapService = iGoodsPurposeMapService;
	}
	public IGoodsFestivalMapService getiGoodsFestivalMapService() {
		return iGoodsFestivalMapService;
	}
	public void setiGoodsFestivalMapService(
			IGoodsFestivalMapService iGoodsFestivalMapService) {
		this.iGoodsFestivalMapService = iGoodsFestivalMapService;
	}
	public IGoodsTargetMapService getiGoodsTargetMapService() {
		return iGoodsTargetMapService;
	}
	public void setiGoodsTargetMapService(
			IGoodsTargetMapService iGoodsTargetMapService) {
		this.iGoodsTargetMapService = iGoodsTargetMapService;
	}
	
	// 接受依赖注入的属性
	private String savePath;

	// 接受依赖注入的方法
	public void setSavePath(String value) {
		this.savePath = value;
	}
	private String getSavePath() throws Exception {
		//return this.getRQ().getRealPath("/");
		return this.getRQ().getSession().getServletContext().getRealPath("/");
	}
	
	private File[] upload;// 实际上传文件
	private String[] uploadContentType;// 文件的内容类型
	private String[] uploadFileName;// 上传文件名
	
	public File[] getUpload() {
		return upload;
	}
	public void setUpload(File[] upload) {
		this.upload = upload;
	}
	public String[] getUploadContentType() {
		return uploadContentType;
	}
	public void setUploadContentType(String[] uploadContentType) {
		this.uploadContentType = uploadContentType;
	}
	public String[] getUploadFileName() {
		return uploadFileName;
	}
	public void setUploadFileName(String[] uploadFileName) {
		this.uploadFileName = uploadFileName;
	}
	
	@Override
	protected String doRequest() {
		logger.info("----------------- Hello DoGoodsUpdateAction -----------------");
		String goodsId = getRQ().getParameter("goodsId");
		String goodsName = getRQ().getParameter("goodsName");
		String goodsPrice = getRQ().getParameter("goodsPrice");
		String[] purposeIds = getRQ().getParameterValues("purposeId");
		String[] festivalIds = getRQ().getParameterValues("festivalId");
		String[] targetIds = getRQ().getParameterValues("targetId");
		String flowerFloridId = getRQ().getParameter("flowerFloridId");
		String flowerCountId = getRQ().getParameter("flowerCountId");
		String sortId = getRQ().getParameter("sortId");
		String remarks = getRQ().getParameter("remarks");
		//String[] productIds = getRQ().getParameterValues("productIds");
		String goodsDescribe = getRQ().getParameter("goodsDescribe");
		String modifiedPicType = getRQ().getParameter("modifiedPicType");
		
		logger.info("---------goodsName:"+goodsName);
		if(purposeIds.length >= 1) {
			for(int i=0;i<purposeIds.length;i++) {
				logger.info("---------purposeId:"+purposeIds[i]);
			}
		}
		if(festivalIds.length >= 1) {
			for(int i=0;i<festivalIds.length;i++) {
				logger.info("---------festivalId:"+festivalIds[i]);
			}
		}
		if(targetIds.length >= 1) {
			for(int i=0;i<targetIds.length;i++) {
				logger.info("---------targetId:"+targetIds[i]);
			}
		}
		logger.info("---------flowerFloridId:"+flowerFloridId);
		logger.info("---------flowerCountId:"+flowerCountId);
		logger.info("---------sortId:"+sortId);
		logger.info("---------remarks:"+remarks);
		logger.info("---------goodsDescribe:"+goodsDescribe);
		
		try {
			FileOutputStream fos = null;
			FileInputStream fis = null;
			
			File[] files = getUpload();
			String[] filesName = getUploadFileName();
			String[] filesType = getUploadContentType();
			
			String targetSavePath = "";
			String picturePath1 = "";
			String picturePath2 = "";
			String picturePath3 = "";
			String targetFileName1 = "";
			String targetFileName2 = "";
			String targetFileName3 = "";

			logger.info("modifiedPicType:"+modifiedPicType);
			
			//先判断是否有新上传的图片
			if(!modifiedPicType.equals("") && modifiedPicType != null) {
				//修改了小图
				if(modifiedPicType.contains("S")) {
					//上传小图
					System.out.println("SavePath------------------"+getSavePath()+ getUploadFileName()[0]);
					targetFileName1 = UUID.randomUUID().toString()+filesName[0].substring(filesName[0].lastIndexOf("."));
					targetSavePath = getSavePath() + "/upload/goodsPicture";
					File targetSavefile = new File(targetSavePath);
					if(!targetSavefile.exists()) {
						targetSavefile.mkdirs();
					}
					picturePath1 = "upload/goodsPicture/"+targetFileName1;
					fos = new FileOutputStream(getSavePath() + picturePath1);
					fis = new FileInputStream(files[0]);
					byte[] buffer = new byte[4];
					int len = 0;
					while ((len = fis.read(buffer)) > 0) {
						fos.write(buffer, 0, len);
					}
					
					if(modifiedPicType.contains("M")) {
						//上传中图
						System.out.println("SavePath------------------"+getSavePath()+ getUploadFileName()[1]);
						targetFileName2 = UUID.randomUUID().toString()+filesName[1].substring(filesName[1].lastIndexOf("."));
						targetSavePath = getSavePath() + "/upload/goodsPicture";
						File targetSavefile2 = new File(targetSavePath);
						if(!targetSavefile2.exists()) {
							targetSavefile2.mkdirs();
						}
						picturePath2 = "upload/goodsPicture/"+targetFileName2;
						fos = new FileOutputStream(getSavePath() + picturePath2);
						fis = new FileInputStream(files[1]);
						byte[] buffer2 = new byte[4];
						int len2 = 0;
						while ((len2 = fis.read(buffer2)) > 0) {
							fos.write(buffer2, 0, len2);
						}
						
						if(modifiedPicType.contains("L")) {
							//上传大图
							System.out.println("SavePath------------------"+getSavePath()+ getUploadFileName()[2]);
							targetFileName3 = UUID.randomUUID().toString()+filesName[2].substring(filesName[2].lastIndexOf("."));
							targetSavePath = getSavePath() + "/upload/goodsPicture";
							File targetSavefile3 = new File(targetSavePath);
							if(!targetSavefile3.exists()) {
								targetSavefile3.mkdirs();
							}
							picturePath3 = "upload/goodsPicture/"+targetFileName3;
							fos = new FileOutputStream(getSavePath() + picturePath3);
							fis = new FileInputStream(files[2]);
							byte[] buffer3 = new byte[4];
							int len3 = 0;
							while ((len3 = fis.read(buffer3)) > 0) {
								fos.write(buffer3, 0, len3);
							}
						}
					}else if(modifiedPicType.contains("L")) {
						//上传大图
						System.out.println("SavePath------------------"+getSavePath()+ getUploadFileName()[1]);
						targetFileName3 = UUID.randomUUID().toString()+filesName[1].substring(filesName[1].lastIndexOf("."));
						targetSavePath = getSavePath() + "/upload/goodsPicture";
						File targetSavefile3 = new File(targetSavePath);
						if(!targetSavefile3.exists()) {
							targetSavefile3.mkdirs();
						}
						picturePath3 = "upload/goodsPicture/"+targetFileName3;
						fos = new FileOutputStream(getSavePath() + picturePath3);
						fis = new FileInputStream(files[1]);
						byte[] buffer3 = new byte[4];
						int len3 = 0;
						while ((len3 = fis.read(buffer3)) > 0) {
							fos.write(buffer3, 0, len3);
						}
					}
				}else if(modifiedPicType.contains("M")) {
					//上传中图
					System.out.println("SavePath------------------"+getSavePath()+ getUploadFileName()[0]);
					targetFileName2 = UUID.randomUUID().toString()+filesName[0].substring(filesName[0].lastIndexOf("."));
					targetSavePath = getSavePath() + "/upload/goodsPicture";
					File targetSavefile2 = new File(targetSavePath);
					if(!targetSavefile2.exists()) {
						targetSavefile2.mkdirs();
					}
					picturePath2 = "upload/goodsPicture/"+targetFileName2;
					fos = new FileOutputStream(getSavePath() + picturePath2);
					fis = new FileInputStream(files[0]);
					byte[] buffer2 = new byte[4];
					int len2 = 0;
					while ((len2 = fis.read(buffer2)) > 0) {
						fos.write(buffer2, 0, len2);
					}
					
					if(modifiedPicType.contains("L")) {
						//上传大图
						System.out.println("SavePath------------------"+getSavePath()+ getUploadFileName()[1]);
						targetFileName3 = UUID.randomUUID().toString()+filesName[1].substring(filesName[1].lastIndexOf("."));
						targetSavePath = getSavePath() + "/upload/goodsPicture";
						File targetSavefile3 = new File(targetSavePath);
						if(!targetSavefile3.exists()) {
							targetSavefile3.mkdirs();
						}
						picturePath3 = "upload/goodsPicture/"+targetFileName3;
						fos = new FileOutputStream(getSavePath() + picturePath3);
						fis = new FileInputStream(files[1]);
						byte[] buffer3 = new byte[4];
						int len3 = 0;
						while ((len3 = fis.read(buffer3)) > 0) {
							fos.write(buffer3, 0, len3);
						}
					}
				}else if(modifiedPicType.contains("L")) {
					//上传大图
					System.out.println("SavePath------------------"+getSavePath()+ getUploadFileName()[0]);
					targetFileName3 = UUID.randomUUID().toString()+filesName[0].substring(filesName[0].lastIndexOf("."));
					targetSavePath = getSavePath() + "/upload/goodsPicture";
					File targetSavefile3 = new File(targetSavePath);
					if(!targetSavefile3.exists()) {
						targetSavefile3.mkdirs();
					}
					picturePath3 = "upload/goodsPicture/"+targetFileName3;
					fos = new FileOutputStream(getSavePath() + picturePath3);
					fis = new FileInputStream(files[0]);
					byte[] buffer3 = new byte[4];
					int len3 = 0;
					while ((len3 = fis.read(buffer3)) > 0) {
						fos.write(buffer3, 0, len3);
					}
				}
			}
			// 注意：流应当关闭。
			if(fos != null) {
				fos.close();
			}
			if(fis != null) {
				fis.close();
			}
			//------------上传修改图片--结束------------------
			
			//继续操作数据库存储
			IOPResultDto<IObjectVO> iopResultDtoGoodsVO = iGoodsService.findById(Integer.valueOf(goodsId));
			GoodsVO goodsVO = null;
			if(iopResultDtoGoodsVO.isSuccess()) {
				goodsVO = iopResultDtoGoodsVO.getFirstResult().getGoodsVOList().get(0);
			}else {
				return ERROR;
			}
			
			FlowerFloridVO flowerFloridVO = new FlowerFloridVO();
			flowerFloridVO.setFlowerFloridId(Integer.parseInt(flowerFloridId));
			FlowerCountVO flowerCountVO = new FlowerCountVO();
			flowerCountVO.setFlowerCountId(Integer.parseInt(flowerCountId));
			
			goodsVO.setGoodsName(goodsName);
			goodsVO.setFlowerFloridVO(flowerFloridVO);
			goodsVO.setFlowerCountVO(flowerCountVO);
			goodsVO.setGoodsPrice(Float.parseFloat(goodsPrice));
			//【商品变更暂时不支持促销】
			//goodsVO.setIsBargin(0);
			goodsVO.setGoodsDescribe(goodsDescribe);
			//【商品变更暂时不支持修改状态】
			//goodsVO.setState(1);
			goodsVO.setSortId(Integer.parseInt(sortId));
			goodsVO.setRemarks(remarks);

			//存入图片
			if(modifiedPicType.contains("S")) {
				goodsVO.setGoodsPictureS(picturePath1);
			}
			if(modifiedPicType.contains("M")) {
				goodsVO.setGoodsPictureM(picturePath2);
			}
			if(modifiedPicType.contains("L")) {
				goodsVO.setGoodsPictureL(picturePath3);
			}
			
			//先存入商品实例
			IOPResultDto<IObjectVO> resultDto = new IOPResultDto<IObjectVO>();
			resultDto = iGoodsService.updateGoods(goodsVO);
			if(resultDto.isSuccess()) {
				int insGoodsId = goodsVO.getGoodsId();
				GoodsVO goodsVOTrans = new GoodsVO();
				goodsVOTrans.setGoodsId(insGoodsId);

				//开始存映射关联表
				//【商品产品关联暂不支持修改】
				
				
				//商品用途关联
					//先删除后添加
				List<GoodsPurposeMapVO> goodsPurposeMapVOListOld = goodsVO.getGoodsPurposeMapVOs();
				IOPResultDto<IObjectVO> dtoGoodsPurposeMapDel = iGoodsPurposeMapService.deleteGoodsPurposeMaps(goodsPurposeMapVOListOld);
				if(dtoGoodsPurposeMapDel.isSuccess() && purposeIds.length >= 1) {
					PurposeVO purposeVO = null;
					GoodsPurposeMapVO goodsPurposeMapVO = null;
					List<GoodsPurposeMapVO> goodsPurposeMapVOList = new ArrayList<GoodsPurposeMapVO>();
					for(int i=0;i<purposeIds.length;i++) {
						purposeVO = new PurposeVO();
						purposeVO.setPurposeId(purposeIds[i]);
						goodsPurposeMapVO = new GoodsPurposeMapVO();
						goodsPurposeMapVO.setGoodsVO(goodsVOTrans);
						goodsPurposeMapVO.setMapId(0);
						goodsPurposeMapVO.setPurposeVO(purposeVO);
						goodsPurposeMapVOList.add(goodsPurposeMapVO);
					}
					if(goodsPurposeMapVOList.size() > 0) {
						IOPResultDto<IObjectVO> dtoGoodsPurposeMap = iGoodsPurposeMapService.addGoodsPurposeMaps(goodsPurposeMapVOList);
						if(!dtoGoodsPurposeMap.isSuccess()) {
							return ERROR;
						}
					}
				}
				//商品节日关联					
					//先删除后添加
				List<GoodsFestivalMapVO> goodsFestivalMapVOListOld = goodsVO.getGoodsFestivalMapVOs();
				IOPResultDto<IObjectVO> dtoGoodsFestivalMapDel = iGoodsFestivalMapService.deleteGoodsFestivalMaps(goodsFestivalMapVOListOld);
				if(dtoGoodsFestivalMapDel.isSuccess() && festivalIds.length >= 1) {
					FestivalVO festivalVO = null;
					GoodsFestivalMapVO goodsFestivalMapVO = null;
					List<GoodsFestivalMapVO> goodsFestivalMapVOList = new ArrayList<GoodsFestivalMapVO>();
					for(int i=0;i<festivalIds.length;i++) {
						festivalVO = new FestivalVO();
						festivalVO.setFestivalId(festivalIds[i]);
						goodsFestivalMapVO = new GoodsFestivalMapVO();
						goodsFestivalMapVO.setGoodsVO(goodsVOTrans);
						goodsFestivalMapVO.setMapId(0);
						goodsFestivalMapVO.setFestivalVO(festivalVO);
						goodsFestivalMapVOList.add(goodsFestivalMapVO);
					}
					if(goodsFestivalMapVOList.size() > 0) {
						IOPResultDto<IObjectVO> dtoGoodsFestivalMap = iGoodsFestivalMapService.addGoodsFestivalMaps(goodsFestivalMapVOList);
						if(!dtoGoodsFestivalMap.isSuccess()) {
							return ERROR;
						}
					}
				}
				//商品赠送对象关联					
					//先删除后添加
				List<GoodsTargetMapVO> goodsTargetMapVOListOld = goodsVO.getGoodsTargetMapVOs();
				IOPResultDto<IObjectVO> dtoGoodsTargetMapDel = iGoodsTargetMapService.deleteGoodsTargetMaps(goodsTargetMapVOListOld);
				if(dtoGoodsTargetMapDel.isSuccess() && targetIds.length >= 1) {
					TargetVO targetVO = null;
					GoodsTargetMapVO goodsTargetMapVO = null;
					List<GoodsTargetMapVO> goodsTargetMapVOList = new ArrayList<GoodsTargetMapVO>();
					for(int i=0;i<targetIds.length;i++) {
						targetVO = new TargetVO();
						targetVO.setTargetId(targetIds[i]);
						goodsTargetMapVO = new GoodsTargetMapVO();
						goodsTargetMapVO.setGoodsVO(goodsVOTrans);
						goodsTargetMapVO.setMapId(0);
						goodsTargetMapVO.setTargetVO(targetVO);
						goodsTargetMapVOList.add(goodsTargetMapVO);
					}
					if(goodsTargetMapVOList.size() > 0) {
						IOPResultDto<IObjectVO> dtoGoodsTargetMap = iGoodsTargetMapService.addGoodsTargetMaps(goodsTargetMapVOList);
						if(!dtoGoodsTargetMap.isSuccess()) {
							return ERROR;
						}
					}
				}
				return SUCCESS;
			}else {
				return ERROR;
			}
		} catch (Exception e) {
			e.printStackTrace();
			return ERROR;
		}
	}

}
